﻿using Freshbox2._0.BLL;
using Freshbox2._0.MODEL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;

namespace Freshbox2._0.UI.Controllers
{
    public class PancakeOrderStaticController : BaseController<presellindent>
    {
        //  煎饼订单

        // GET: /PancakeOrderStatic/

        public override BLL.BaseBll<presellindent> bll
        {
            get
            {
                return new PresellIndentBll();
            }

        }
        PresellIndentDetailBll orderDetailsBll = new PresellIndentDetailBll();
        PresellWaresBll wareBll = new PresellWaresBll();
        public class IndentModel
        {
            public int userCode;         
            public string orderCode;
            public string orderTime;
            public decimal orderSumprice;
            public string wechatName;
            public string userPhone;
            public string pindentTakeTime;
            public string preferentialMoney;//优惠金额
            public string storeName;//消费店铺
            public string isRepeat;//是否复购
            public string pindentStatus;

        }

        /// <summary>
        /// 订单详细信息
        /// </summary>
        public class OrderDetailModel
        {
            public string orderCode;
            public int waresCode;
            public string waresName;
            public int waresNumber;
            public decimal waresPrice;
            public decimal TotalPrice;
            public string standard;

        }


        UserBll userbll = new UserBll();

        StoreBll storeBll = new StoreBll();

        PwaresStandardBll standardBll = new PwaresStandardBll();

        public ActionResult GetOrderByEf(string date1, string date2, string adminUsername, string userPhone, string storeid, int page, int rows)
        {
            int count;

            List<IndentModel> OrderList = new List<IndentModel>();

            //1. 查询支付成功的订单     这里多种状态                        
            Expression<Func<presellindent, bool>> condition = null;  //   暂定  为1  的是支付成功的
            
            //默认情况   默认查询本天的所有记录  
            if (string.IsNullOrEmpty(date1) && string.IsNullOrEmpty(date2) && string.IsNullOrEmpty(storeid))
            {
                DateTime todayNow = DateTime.Now;
                DateTime midnight = DateTime.Now;
                midnight = midnight.Date;
                condition = x => x.pindentCreatime <= todayNow && x.pindentCreatime >= midnight;
            }
            //只按照店铺查询 默认是今天某店铺的订单情况
            if (string.IsNullOrEmpty(date1) && string.IsNullOrEmpty(date2) && storeid != "")
            {
                int shopid = Convert.ToInt32(storeid);
                DateTime todayNow = DateTime.Now;
                DateTime midnight = DateTime.Now;
                midnight = midnight.Date;
                condition = x => x.pindentCreatime <= todayNow && x.pindentCreatime >= midnight && x.pindentStoreId == shopid;
            }
            //按照时间和店铺查询
            if (date1 != null && date1 != "" && date2 != null && date2 != "" && storeid != "")
            {
                int shopid = Convert.ToInt32(storeid);
                DateTime datetime1 = DateTime.Parse(date1);
                DateTime datetime2 = DateTime.Parse(date2).AddDays(1);
                condition = x => x.pindentCreatime >= datetime1 && x.pindentCreatime <= datetime2 && x.pindentStoreId == shopid;
            }
            //只按照时间查询 
            if (date1 != null && date1 != "" && date2 != null && date2 != "" && storeid == "")
            {
                DateTime datetime1 = DateTime.Parse(date1);
                DateTime datetime2 = DateTime.Parse(date2).AddDays(1);
                condition = x =>  x.pindentCreatime >= datetime1 && x.pindentCreatime <= datetime2;
            }
            var listOrder = bll.Search(condition, true, x => x.pindentTakeTime, rows, page, out count).Select(x => new { x.pindentId,x.pindentStatus, x.user.wechatName,x.user.userPhone,x.pindentUserCode, x.pindentPrice, x.discountMoney, x.pindentStoreId, x.pindentCreatime, x.pindentTakeTime}).ToList();

            foreach (var oneOrder in listOrder)
            {
                Expression<Func<user, bool>> condition2 = x => true;

                //如果所有条件为空  
                if (string.IsNullOrEmpty(adminUsername) && string.IsNullOrEmpty(userPhone))
                {
                    string shopName = "";

                    shopName = storeBll.Search(x => x.storeid == oneOrder.pindentStoreId).FirstOrDefault().storeName;


                    //代表没有该用户的信息 可能是支付宝支付 或者 没有关注公众号   
                    if (oneOrder.pindentUserCode == 0)
                    {
                        IndentModel orderModel = new IndentModel();
                        orderModel.userCode = 0;
                        orderModel.userPhone = "未知";
                        orderModel.wechatName = "未知";
                        orderModel.isRepeat = "未知";
                        orderModel.orderCode = oneOrder.pindentId;
                        orderModel.orderTime = Convert.ToDateTime(oneOrder.pindentCreatime).ToString();//oneOrder.orderTime.ToShortDateString();
                        orderModel.orderSumprice = Convert.ToDecimal(oneOrder.pindentPrice);
                        orderModel.pindentTakeTime = Convert.ToDateTime(oneOrder.pindentTakeTime).ToString();
                        orderModel.storeName = shopName;
                        orderModel.pindentStatus=oneOrder.pindentStatus==0?"未支付":oneOrder.pindentStatus==1?"支付完成，正在制作中":oneOrder.pindentStatus==2?"制作完成，待取餐":"取餐成功，交易完成";
                        orderModel.preferentialMoney = oneOrder.discountMoney.ToString();
                        OrderList.Add(orderModel);     //  return Json(new { rows = OrderList, total = count - 1 });
                    }
                    ////代表有该用户的信息
                    else
                    {
                        IndentModel orderModel = new IndentModel();
                        orderModel.userCode = oneOrder.pindentUserCode;
                        var a = bll.Search(x => x.pindentUserCode == oneOrder.pindentUserCode).ToList();
                        if (a.Count() >= 2)
                        {
                            orderModel.isRepeat = "是";
                        }
                        else
                        {
                            orderModel.isRepeat = "否";
                        }

                        orderModel.orderCode = oneOrder.pindentId;
                        orderModel.orderTime = Convert.ToDateTime(oneOrder.pindentCreatime).ToString();
                        orderModel.wechatName = oneOrder.wechatName;
                        orderModel.userPhone = oneOrder.userPhone;
                        orderModel.orderSumprice = Convert.ToDecimal(oneOrder.pindentPrice);
                        orderModel.preferentialMoney = oneOrder.discountMoney.ToString();
                        orderModel.pindentStatus = oneOrder.pindentStatus == 0 ? "未支付" : oneOrder.pindentStatus == 1 ? "支付完成，正在制作中" : oneOrder.pindentStatus == 2 ? "制作完成，待取餐" : "取餐成功，交易完成";

                        orderModel.pindentTakeTime = Convert.ToDateTime(oneOrder.pindentTakeTime).ToString();
                        orderModel.storeName = shopName;
                        OrderList.Add(orderModel);
                    }
                }
                //手机号查询条件
                if (userPhone != "" && userPhone != null && string.IsNullOrEmpty(adminUsername))
                {
                    if (oneOrder.pindentUserCode != 0)
                    {
                        condition2 = x => x.userCode == oneOrder.pindentUserCode && x.userPhone.Contains(userPhone) && (x.userPhone != null && x.userPhone != "");
                    }
                    else
                    {
                        continue;
                    }
                    //查询订单对应的用户信息
                    var list2 = userbll.Search(condition2).Select(x => new { x.userCode, x.userName, x.userPhone, x.wechatName }).ToList();
                    //代表有该用户的信息
                    if (list2.Count != 0)
                    {
                        string shopName = storeBll.Search(x => x.storeid == oneOrder.pindentStoreId).FirstOrDefault().storeName;
                        IndentModel orderModel = new IndentModel();
                        orderModel.userCode = list2.FirstOrDefault().userCode;

                        orderModel.orderCode = oneOrder.pindentId;
                        orderModel.orderTime = Convert.ToDateTime(oneOrder.pindentCreatime).ToString();
                        orderModel.wechatName = list2.FirstOrDefault().wechatName;
                        orderModel.userPhone = list2.FirstOrDefault().userPhone;
                        orderModel.orderSumprice = Convert.ToDecimal(oneOrder.pindentPrice);
                        orderModel.preferentialMoney = oneOrder.discountMoney.ToString();
                        orderModel.pindentStatus = oneOrder.pindentStatus == 0 ? "未支付" : oneOrder.pindentStatus == 1 ? "支付完成，正在制作中" : oneOrder.pindentStatus == 2 ? "制作完成，待取餐" : "取餐成功，交易完成";

                        var a = bll.Search(x => x.pindentUserCode == orderModel.userCode).ToList();
                        if (a.Count() >= 2)
                        {
                            orderModel.isRepeat = "是";
                        }
                        else
                        {
                            orderModel.isRepeat = "否";
                        }
                        orderModel.pindentTakeTime = Convert.ToDateTime(oneOrder.pindentTakeTime).ToString();
                        orderModel.storeName = shopName;
                        OrderList.Add(orderModel);
                    }
                }

                //微信名称 查询条件
                if (adminUsername != "" && adminUsername != null && string.IsNullOrEmpty(userPhone))
                {
                    if (oneOrder.pindentUserCode != 0)
                    {
                        condition2 = x => x.userCode == oneOrder.pindentUserCode && x.wechatName.Contains(adminUsername) && (x.wechatName != null && x.wechatName != "");
                    }
                    else
                    {
                        continue;
                    }
                    //查询订单对应的用户信息
                    var list2 = userbll.Search(condition2).Select(x => new { x.userCode, x.userName, x.userPhone, x.wechatName }).ToList();
                    //代表有该用户的信息
                    if (list2.Count != 0)
                    {
                        string shopName = storeBll.Search(x => x.storeid == oneOrder.pindentStoreId).FirstOrDefault().storeName;
                        IndentModel orderModel = new IndentModel();
                        orderModel.userCode = list2.FirstOrDefault().userCode;
                        orderModel.wechatName = list2.FirstOrDefault().wechatName;
                        orderModel.orderCode = oneOrder.pindentId;
                        orderModel.pindentStatus = oneOrder.pindentStatus == 0 ? "未支付" : oneOrder.pindentStatus == 1 ? "支付完成，正在制作中" : oneOrder.pindentStatus == 2 ? "制作完成，待取餐" : "取餐成功，交易完成";

                        var a = bll.Search(x => x.pindentUserCode == orderModel.userCode).ToList();
                        if (a.Count() >= 2)
                        {
                            orderModel.isRepeat = "是";
                        }
                        else
                        {
                            orderModel.isRepeat = "否";
                        }
                        orderModel.orderTime = Convert.ToDateTime(oneOrder.pindentCreatime).ToString();
                        orderModel.wechatName = list2.FirstOrDefault().wechatName;
                        orderModel.userPhone = list2.FirstOrDefault().userPhone;
                        orderModel.orderSumprice = Convert.ToDecimal(oneOrder.pindentPrice);
                        orderModel.preferentialMoney = oneOrder.discountMoney.ToString();
                        orderModel.pindentTakeTime = Convert.ToDateTime(oneOrder.pindentTakeTime).ToString();
                        orderModel.storeName = shopName;
                        OrderList.Add(orderModel);
                    }
                }

                //微信名称 跟 手机号查询  查询条件
                if (adminUsername != "" && adminUsername != null && userPhone != "" && userPhone != null)
                {
                    if (oneOrder.pindentUserCode != 0)
                    {
                        condition2 = x => x.userCode == oneOrder.pindentUserCode && x.wechatName.Contains(adminUsername) && x.userPhone.Contains(userPhone) && (x.userPhone != null && x.userPhone != "");
                    }
                    else
                    {
                        continue;
                    }
                    //查询订单对应的用户信息
                    var list2 = userbll.Search(condition2).Select(x => new { x.userCode, x.userName, x.userPhone, x.wechatName }).ToList();
                    //代表有该用户的信息
                    if (list2.Count != 0)
                    {
                        string shopName = storeBll.Search(x => x.storeid == oneOrder.pindentStoreId).FirstOrDefault().storeName;
                        IndentModel orderModel = new IndentModel();
                        orderModel.userCode = list2.FirstOrDefault().userCode;

                        var a = bll.Search(x => x.pindentUserCode == orderModel.userCode).ToList();
                        if (a.Count() >= 2)
                        {
                            orderModel.isRepeat = "是";
                        }
                        else
                        {
                            orderModel.isRepeat = "否";
                        }
                       
                        orderModel.orderCode = oneOrder.pindentId;
                        orderModel.orderTime = Convert.ToDateTime(oneOrder.pindentCreatime).ToString();
                        orderModel.wechatName = list2.FirstOrDefault().wechatName;
                        orderModel.userPhone = list2.FirstOrDefault().userPhone;
                        orderModel.orderSumprice = Convert.ToDecimal(oneOrder.pindentPrice);
                        orderModel.preferentialMoney = oneOrder.discountMoney.ToString();
                        orderModel.pindentTakeTime = Convert.ToDateTime(oneOrder.pindentTakeTime).ToString();
                        orderModel.storeName = shopName;
                        orderModel.pindentStatus = oneOrder.pindentStatus == 0 ? "未支付" : oneOrder.pindentStatus == 1 ? "支付完成，正在制作中" : oneOrder.pindentStatus == 2 ? "制作完成，待取餐" : "取餐成功，交易完成";

                        OrderList.Add(orderModel);
                    }
                }
            }//foreach 结束
            return Json(new { rows = OrderList, total = count });
        }


        public ActionResult GetDetailByID(string orderCode, string date1, string date2)
        {

            var list = orderDetailsBll.Search(x => x.pindentId == orderCode).ToList().Select(x => new { x.pindentId, x.pwaresId, x.pwaresNum,x.pwaresStandardId,}).ToList();

            var listOrderDetail = new List<OrderDetailModel>();

            foreach (var oneDetailCode in list)
            {
                
                OrderDetailModel detail = new OrderDetailModel();

                var list2 = wareBll.Search(x => x.pwaresId == oneDetailCode.pwaresId).Select(x => new { x.pwaresId,x.pwaresName, x.pwaresPrice,x.pwaresNum }).ToList();
                if (list2.Count != 0)
                {
                    //var a = standardBll.Search(y => y.pwaresStandardId == oneDetailCode.pwaresStandardId).Select(x => new { x.pwaresStandardId, x.pstandardName }).ToList();
                    //if (a.Count != 0)
                    //{
                    //    detail.standard = a.FirstOrDefault().pstandardName;
                    //}
                    //else
                    //{
                    //    detail.standard = "";
                    //}
                    
                    detail.waresCode = oneDetailCode.pwaresId;
                    detail.waresName = list2.FirstOrDefault().pwaresName;
                    detail.waresNumber = Convert.ToInt32(oneDetailCode.pwaresNum);
                    detail.waresPrice = Convert.ToDecimal(list2.FirstOrDefault().pwaresPrice);
                    detail.TotalPrice = Convert.ToDecimal(list2.FirstOrDefault().pwaresPrice * oneDetailCode.pwaresNum);
                    listOrderDetail.Add(detail);
                }
            }
            return Json(listOrderDetail);
        }

        public decimal Shouldmoney = 0.00m;//应收金额 

        public decimal Discountmoney = 0.00m;//优惠金额

        public decimal Actualmoney = 0.00m;//实际收入金额

        public decimal PerPrice; //客单价

        public int OrderSum;  //客单量  PerPrice OrderSum

        /// <summary>
        /// 计算销售总和   客单价  成交量
        /// </summary>
        /// <param name="storeid"></param>
        /// <param name="date1"></param>
        /// <param name="date2"></param>
        /// <param name="adminUsername"></param>
        /// <param name="userPhone"></param>
        /// <returns></returns> 
        public ActionResult GetOrderAll(string storeid, string date1, string date2, string adminUsername, string userPhone)
        {
          
            List<IndentModel> OrderList = new List<IndentModel>();

            //1. 查询支付成功的订单     这里多种状态                        
            Expression<Func<presellindent, bool>> condition = null;  //   暂定  为1  的是支付成功的

            //默认情况   默认查询本天的所有记录  
            if (string.IsNullOrEmpty(date1) && string.IsNullOrEmpty(date2) && string.IsNullOrEmpty(storeid))
            {
                DateTime todayNow = DateTime.Now;
                DateTime midnight = DateTime.Now;
                midnight = midnight.Date;
                condition = x => x.pindentCreatime <= todayNow && x.pindentCreatime >= midnight && x.pindentStatus!=0;
            }
            //只按照店铺查询 默认是今天某店铺的订单情况
            if (string.IsNullOrEmpty(date1) && string.IsNullOrEmpty(date2) && storeid != "")
            {
                int shopid = Convert.ToInt32(storeid);
                DateTime todayNow = DateTime.Now;
                DateTime midnight = DateTime.Now;
                midnight = midnight.Date;
                condition = x => x.pindentCreatime <= todayNow && x.pindentCreatime >= midnight && x.pindentStoreId == shopid && x.pindentStatus != 0;
            }
            //按照时间和店铺查询
            if (date1 != null && date1 != "" && date2 != null && date2 != "" && storeid != "")
            {
                int shopid = Convert.ToInt32(storeid);
                DateTime datetime1 = DateTime.Parse(date1);
                DateTime datetime2 = DateTime.Parse(date2).AddDays(1);
                condition = x => x.pindentCreatime >= datetime1 && x.pindentCreatime <= datetime2 && x.pindentStoreId == shopid && x.pindentStatus != 0;
            }
            //只按照时间查询 
            if (date1 != null && date1 != "" && date2 != null && date2 != "" && storeid == "")
            {
                DateTime datetime1 = DateTime.Parse(date1);
                DateTime datetime2 = DateTime.Parse(date2).AddDays(1);
                condition = x => x.pindentCreatime >= datetime1 && x.pindentCreatime <= datetime2 && x.pindentStatus != 0;
            }

            var listOrder = bll.Search(condition).Select(x => new { x.pindentId, x.pindentStatus, x.user.wechatName, x.user.userPhone, x.pindentUserCode, x.pindentPrice, x.discountMoney, x.pindentStoreId, x.pindentCreatime, x.pindentTakeTime }).ToList();

            foreach (var oneOrder in listOrder)
            {
                Expression<Func<user, bool>> condition2 = x => true;

                //如果所有条件为空  
                if (string.IsNullOrEmpty(adminUsername) && string.IsNullOrEmpty(userPhone))
                {
                    string shopName = "";

                    shopName = storeBll.Search(x => x.storeid == oneOrder.pindentStoreId).FirstOrDefault().storeName;


                    //代表没有该用户的信息 可能是支付宝支付 或者 没有关注公众号   
                    if (oneOrder.pindentUserCode == 0)
                    {
                        IndentModel orderModel = new IndentModel();
                        orderModel.userCode = 0;
                        orderModel.userPhone = "未知";
                        orderModel.wechatName = "未知";
                        orderModel.isRepeat = "未知";
                        orderModel.orderCode = oneOrder.pindentId;
                        orderModel.orderTime = Convert.ToDateTime(oneOrder.pindentCreatime).ToString();//oneOrder.orderTime.ToShortDateString();
                        orderModel.orderSumprice = Convert.ToDecimal(oneOrder.pindentPrice);
                        orderModel.pindentTakeTime = Convert.ToDateTime(oneOrder.pindentTakeTime).ToString();
                        
                        orderModel.pindentStatus=oneOrder.pindentStatus==0?"未支付":oneOrder.pindentStatus==1?"支付完成，正在制作中":oneOrder.pindentStatus==2?"制作完成，待取餐":"取餐成功，交易完成";
                        orderModel.preferentialMoney = oneOrder.discountMoney.ToString();


                        OrderList.Add(orderModel);

                        Actualmoney = Actualmoney + Convert.ToDecimal(orderModel.orderSumprice);
                        Discountmoney = Discountmoney + Convert.ToDecimal(orderModel.preferentialMoney);
                        Shouldmoney = Actualmoney + Discountmoney;
                        OrderSum = listOrder.Count();
                        PerPrice = Shouldmoney / OrderSum;
                    }
                    else
                    {
                        IndentModel orderModel = new IndentModel();
                        orderModel.userCode = oneOrder.pindentUserCode;
                        var a = bll.Search(x => x.pindentUserCode == oneOrder.pindentUserCode).ToList();
                        if (a.Count() >= 2)
                        {
                            orderModel.isRepeat = "是";
                        }
                        else
                        {
                            orderModel.isRepeat = "否";
                        }

                        orderModel.orderCode = oneOrder.pindentId;
                        orderModel.orderTime = Convert.ToDateTime(oneOrder.pindentCreatime).ToString();
                        orderModel.wechatName = oneOrder.wechatName;
                        orderModel.userPhone = oneOrder.userPhone;
                        orderModel.orderSumprice = Convert.ToDecimal(oneOrder.pindentPrice);
                        orderModel.preferentialMoney = oneOrder.discountMoney.ToString();
                        orderModel.pindentStatus = oneOrder.pindentStatus == 0 ? "未支付" : oneOrder.pindentStatus == 1 ? "支付完成，正在制作中" : oneOrder.pindentStatus == 2 ? "制作完成，待取餐" : "取餐成功，交易完成";

                        orderModel.pindentTakeTime = Convert.ToDateTime(oneOrder.pindentTakeTime).ToString();
                        orderModel.storeName = shopName;
                        OrderList.Add(orderModel);

                        Actualmoney = Actualmoney + Convert.ToDecimal(orderModel.orderSumprice);
                        Discountmoney = Discountmoney + Convert.ToDecimal(orderModel.preferentialMoney);
                        Shouldmoney = Actualmoney + Discountmoney;
                        OrderSum = listOrder.Count();
                        PerPrice = Shouldmoney / OrderSum;
                    }
               
                }
                //手机号查询条件
                if (userPhone != "" && userPhone != null && string.IsNullOrEmpty(adminUsername))
                {
                    if (oneOrder.pindentUserCode != 0)
                    {
                        condition2 = x => x.userCode == oneOrder.pindentUserCode && x.userPhone.Contains(userPhone) && (x.userPhone != null && x.userPhone != "");
                    }
                    else
                    {
                        continue;
                    }
                    //查询订单对应的用户信息
                    var list2 = userbll.Search(condition2).Select(x => new { x.userCode, x.userName, x.userPhone, x.wechatName }).ToList();
                    //代表有该用户的信息
                    if (list2.Count != 0)
                    {
                        string shopName = storeBll.Search(x => x.storeid == oneOrder.pindentStoreId).FirstOrDefault().storeName;
                        IndentModel orderModel = new IndentModel();
                        orderModel.userCode = list2.FirstOrDefault().userCode;

                        orderModel.orderCode = oneOrder.pindentId;
                        orderModel.orderTime = Convert.ToDateTime(oneOrder.pindentCreatime).ToString();
                        orderModel.wechatName = list2.FirstOrDefault().wechatName;
                        orderModel.userPhone = list2.FirstOrDefault().userPhone;
                        orderModel.orderSumprice = Convert.ToDecimal(oneOrder.pindentPrice);
                        orderModel.preferentialMoney = oneOrder.discountMoney.ToString();
                        orderModel.pindentStatus = oneOrder.pindentStatus == 0 ? "未支付" : oneOrder.pindentStatus == 1 ? "支付完成，正在制作中" : oneOrder.pindentStatus == 2 ? "制作完成，待取餐" : "取餐成功，交易完成";

                        var a = bll.Search(x => x.pindentUserCode == orderModel.userCode).ToList();
                        if (a.Count() >= 2)
                        {
                            orderModel.isRepeat = "是";
                        }
                        else
                        {
                            orderModel.isRepeat = "否";
                        }
                        orderModel.pindentTakeTime = Convert.ToDateTime(oneOrder.pindentTakeTime).ToString();
                        orderModel.storeName = shopName;
                        OrderList.Add(orderModel);
                        Actualmoney = Actualmoney + Convert.ToDecimal(orderModel.orderSumprice);
                        Discountmoney = Discountmoney + Convert.ToDecimal(orderModel.preferentialMoney);
                        Shouldmoney = Actualmoney + Discountmoney;
                        OrderSum = listOrder.Count();
                        PerPrice = Shouldmoney / OrderSum;
                    }
                }

                //微信名称 查询条件
                if (adminUsername != "" && adminUsername != null && string.IsNullOrEmpty(userPhone))
                {
                    if (oneOrder.pindentUserCode != 0)
                    {
                        condition2 = x => x.userCode == oneOrder.pindentUserCode && x.wechatName.Contains(adminUsername) && (x.wechatName != null && x.wechatName != "");
                    }
                    else
                    {
                        continue;
                    }
                    //查询订单对应的用户信息
                    var list2 = userbll.Search(condition2).Select(x => new { x.userCode, x.userName, x.userPhone, x.wechatName }).ToList();
                    //代表有该用户的信息
                    if (list2.Count != 0)
                    {
                        string shopName = storeBll.Search(x => x.storeid == oneOrder.pindentStoreId).FirstOrDefault().storeName;
                        IndentModel orderModel = new IndentModel();
                        orderModel.userCode = list2.FirstOrDefault().userCode;
                        orderModel.wechatName = list2.FirstOrDefault().wechatName;
                        orderModel.orderCode = oneOrder.pindentId;
                        orderModel.pindentStatus = oneOrder.pindentStatus == 0 ? "未支付" : oneOrder.pindentStatus == 1 ? "支付完成，正在制作中" : oneOrder.pindentStatus == 2 ? "制作完成，待取餐" : "取餐成功，交易完成";

                        var a = bll.Search(x => x.pindentUserCode == orderModel.userCode).ToList();
                        if (a.Count() >= 2)
                        {
                            orderModel.isRepeat = "是";
                        }
                        else
                        {
                            orderModel.isRepeat = "否";
                        }
                        orderModel.orderTime = Convert.ToDateTime(oneOrder.pindentCreatime).ToString();
                        orderModel.wechatName = list2.FirstOrDefault().wechatName;
                        orderModel.userPhone = list2.FirstOrDefault().userPhone;
                        orderModel.orderSumprice = Convert.ToDecimal(oneOrder.pindentPrice);
                        orderModel.preferentialMoney = oneOrder.discountMoney.ToString();
                        orderModel.pindentTakeTime = Convert.ToDateTime(oneOrder.pindentTakeTime).ToString();
                        orderModel.storeName = shopName;
                        OrderList.Add(orderModel);
                        Actualmoney = Actualmoney + Convert.ToDecimal(orderModel.orderSumprice);
                        Discountmoney = Discountmoney + Convert.ToDecimal(orderModel.preferentialMoney);
                        Shouldmoney = Actualmoney + Discountmoney;
                        OrderSum = listOrder.Count();
                        PerPrice = Shouldmoney / OrderSum;
                    }
                }

                //微信名称 跟 手机号查询  查询条件
                if (adminUsername != "" && adminUsername != null && userPhone != "" && userPhone != null)
                {
                    if (oneOrder.pindentUserCode != 0)
                    {
                        condition2 = x => x.userCode == oneOrder.pindentUserCode && x.wechatName.Contains(adminUsername) && x.userPhone.Contains(userPhone) && (x.userPhone != null && x.userPhone != "");
                    }
                    else
                    {
                        continue;
                    }
                    //查询订单对应的用户信息
                    var list2 = userbll.Search(condition2).Select(x => new { x.userCode, x.userName, x.userPhone, x.wechatName }).ToList();
                    //代表有该用户的信息
                    if (list2.Count != 0)
                    {
                        string shopName = storeBll.Search(x => x.storeid == oneOrder.pindentStoreId).FirstOrDefault().storeName;
                        IndentModel orderModel = new IndentModel();
                        orderModel.userCode = list2.FirstOrDefault().userCode;

                        var a = bll.Search(x => x.pindentUserCode == orderModel.userCode).ToList();
                        if (a.Count() >= 2)
                        {
                            orderModel.isRepeat = "是";
                        }
                        else
                        {
                            orderModel.isRepeat = "否";
                        }

                        orderModel.orderCode = oneOrder.pindentId;
                        orderModel.orderTime = Convert.ToDateTime(oneOrder.pindentCreatime).ToString();
                        orderModel.wechatName = list2.FirstOrDefault().wechatName;
                        orderModel.userPhone = list2.FirstOrDefault().userPhone;
                        orderModel.orderSumprice = Convert.ToDecimal(oneOrder.pindentPrice);
                        orderModel.preferentialMoney = oneOrder.discountMoney.ToString();
                        orderModel.pindentTakeTime = Convert.ToDateTime(oneOrder.pindentTakeTime).ToString();
                        orderModel.storeName = shopName;
                        orderModel.pindentStatus = oneOrder.pindentStatus == 0 ? "未支付" : oneOrder.pindentStatus == 1 ? "支付完成，正在制作中" : oneOrder.pindentStatus == 2 ? "制作完成，待取餐" : "取餐成功，交易完成";

                        OrderList.Add(orderModel);
                        Actualmoney = Actualmoney + Convert.ToDecimal(orderModel.orderSumprice);
                        Discountmoney = Discountmoney + Convert.ToDecimal(orderModel.preferentialMoney);
                        Shouldmoney = Actualmoney + Discountmoney;
                        OrderSum = listOrder.Count();
                        PerPrice = Shouldmoney / OrderSum;
                    }
                }

            }//foreach 结束  Shouldmoney.ToString("#0.00")  (Convert.ToDecimal(Discountmoney) +Convert.ToDecimal( Actualmoney)).ToString("#0.00"),
            return Json(new { rows = OrderList, Shouldmoney = Shouldmoney, OrderSum = OrderSum, Discountmoney = Discountmoney.ToString("#0.00"), Actualmoney = Actualmoney.ToString("#0.00") }, JsonRequestBehavior.AllowGet);
        }




    }
}
